package com.example.demo2022.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2022.entity.LoginLog;
import com.example.demo2022.mapper.LoginLogMapper;
import com.example.demo2022.service.ILoginLogService;
import com.example.demo2022.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/************************************
 * @PACKAGE : com.example.demo2022.service.impl
 * @Author    :  叫我老伯
 * @Date    : 2022/5/31 15:16
 * @Desc    : 往登录表中加入字段
 ***********************************/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public void saveLoginLog(long userId){
      LoginLog loginLog  =  new LoginLog();
      loginLog.setLoginTime(LocalDateTime.now());
      loginLog.setUserId(userId);

        //获取ip
        loginLog.setIp(RequestUtil.getIPAddress(request));
        //获取浏览器信息
        loginLog.setBrowserName(RequestUtil.getOsAndBrowserInfo(request));
        this.save(loginLog);
    }



}
